/**
 * 打开住院查询窗口
 */
var SelectLiveInWindow;
function createSelectLiveInWindow(){
	SelectLiveInWindow = $("<div/>").window({
		title:'住院查询',
		href:'to_queryPatientByLike_forTally',
		width:950,
		height:500,
		modal:true,
		collapsible:false,
		minimizable:false,
		maximizable:false,
		resizable:false,
		iconCls:'icon-search',
		onClose:function(){
			$(this).dialog('destroy');
		}
	});
	$("#LiveInSelect").window("options").SelectLiveIn = "toTally";
}
/**
 * 模糊查询住院By LIVEid
 */
function fuzzyFindPatientByLiveID(){
	var likeID = $("#livePatient_LiveIDinput").val();
	if(likeID==''){
		
	}else{
		$("#livePatient_queryTable").datagrid({
			url:'to_queryPatientByLikeIDForTally',
			queryParams:{
				"likeID":likeID
			}
		});
	}
}
/**
 * 模糊查询住院By NAME
 */
function fuzzyFindPatientByName() {
	var likeName = $("#livePatient_Nameinput").val();
	if (likeName == '') {

	} else {
		$("#livePatient_queryTable").datagrid({
			url : 'to_queryPatientByLikeNameForTally',
			queryParams : {
				"likeName" : likeName
			}
		});
	}
}
/**
 * 判断回填数据至哪里
 */
function decideToReturnLiveInInfo(){
	var liveInIf = $("#LiveInSelect").window("options").SelectLiveIn;
	if(liveInIf == "toTally"){
		writePatientInfoToTally();
	}else if(liveInIf == "toLiveInPay"){
		returnLiveInInfoToPayTalltPage();
	}else if(liveInIf == "toFees"){
		returnInfoToAllFeesPage();
	}else if(liveInIf == "toExchangeBed"){
		returnInfoToExchangeBed();
	}
}
/**
 * 回填病人信息
 */
function writePatientInfoToTally(){
	var row = $("#livePatient_queryTable").datagrid("getSelected");

	$("#tally_LivePatientID").textbox('setValue', row.liveid8);
	$("#tally_LivePatientName").textbox('setValue', row.patientname8);
	$("#tally_LivePatientType").textbox('setValue', row.livetypename);
	
	$.getJSON('to_queryBalance',{
		"liveID":row.liveid8
	},function(data){
		$("#tally_LivePatientDeposit").textbox('setValue', data.deposit);
		$("#tally_LivePatientNopay").textbox('setValue', data.nopay);
		$("#tally_LivePatientBalance").textbox('setValue', data.balance);
	});
	SelectLiveInWindow.window('destroy');
}
//打开收费项目窗口  自定义id 回传门诊药品datagrid
function openChargeProjectWindowFromOuterx(){
	$('#chargeProjectWindow').window('open');
	$("#ChargeProjectSelect").window("options").ChargeProject = "outerx";
}
//回填门诊药品信息
function selectChargeProjectToOutErx(){
	var row = $("#chargeProject_Table").datagrid("getSelected");
	$("#out_drugs_name").textbox('setValue', row.CHAEGENAME);
	$("#out_drugs_num").numberspinner('setValue', 1);
	$("#out_drugs_price").textbox('setValue', row.PRICE3);
	$("#out_drugs_sum").textbox('setValue', row.PRICE3);
	$('#chargeProjectWindow').window('close');
}

//打开收费项目窗口  自定义id 回传医疗项目datagrid
function openProjectWindowFromOutProject(){
	$('#chargeProjectWindow').window('open');
	$('#ChargeProjectSelect').window("options").ChargeProject = "outproject";
}


//医疗项目信息回填
function selectProjectToOutProject(){
	var row = $("#chargeProject_Table").datagrid("getSelected");
	$("#out_project_name").textbox('setValue', row.CHAEGENAME);
	$("#out_project_num").numberspinner('setValue', 1);
	$("#out_project_price").textbox('setValue', row.PRICE3);
	$("#out_project_sum").textbox('setValue', row.PRICE3);
	$('#chargeProjectWindow').window('close');
}


/**
 * 判断回填数据至哪里
 */
function decideToReturnChargeProjectInfo(){
	var projectS = $("#ChargeProjectSelect").window("options").ChargeProject;
	if(projectS == "toTally"){
		selectChargeProjectToTally();
	}else if(projectS == "outerx"){
		selectChargeProjectToOutErx();
	}else if(projectS =="outproject"){
		selectProjectToOutProject();
	}
}


/**
 * 打开选择收费项目窗口
 */
function openSelectTallyDetialWindow() {
	$('#windowChargeProject').window('open');
	$("#ChargeProjectSelect").window("options").ChargeProject = "toTally";
}
/**
 * 生成账单号
 */
function createTallyID(){
	$.getJSON("to_createTallyID",{},function(date){
		$("#tally_id").textbox('setValue',date);
	});
}
/**
 * 查询收费项目
 */
function fuzzyquerychargeProject() {
	var tree = $("#chargeTypeTree").tree('getSelected');
	$("#chargeProject_Table").datagrid({
		url : 'to_querychargeByID',
		queryParams : {
			"typeID" : tree.id
		}
	});
	$("#chargeProjectName").val('');
}
function queryChargeProject() {

	var name = $("#chargeProjectName").val();
	if (name != "") {
		$("#chargeProject_Table").datagrid({
			url : 'to_querychargeByName',
			queryParams : {
				"name" : name
			}
		});
	}
}
/**
 * 回填收费项目信息
 */
function selectChargeProjectToTally() {
	var row = $("#chargeProject_Table").datagrid("getSelected");

	$("#tally_chargeProjectName").textbox('setValue', row.CHAEGENAME);
	$("#tally_chargeProjectPrice").textbox('setValue', row.PRICE3);
	$("#tally_chargeProjectSum").textbox('setValue', row.PRICE3);
	$("#tally_chargeProjectNumber").numberspinner('setValue', 1);
	$('#windowChargeProject').window('close');
}
/**
 * 计算账单项目总计
 */
function countTotalTallyDetial() {

	var price = $("#tally_chargeProjectPrice").textbox('getValue');
	var number = $("#tally_chargeProjectNumber").numberspinner('getValue');
	var Total;
	if (number <= 0) {
		$.messager.alert('警告', '数量不能少于1!', 'info');
		$("#tally_chargeProjectNumber").numberspinner('setValue', 1);
		$("#tally_chargeProjectSum").textbox('setValue', price);
	} else {
		Total = price * number;
		$("#tally_chargeProjectSum").textbox('setValue', Total);
	}
}
/**
 * 添加收费账单零细
 */
function addTallyDetial() {

	var p = $("#tally_chargeProjectPrice").textbox('getValue');
	if (p == "") {
		$.messager.alert('提示', '请先选择收费项目!', 'info');
	} else {
		var row = $("#chargeProject_Table").datagrid("getSelected");
		var total = $("#tally_chargeProjectSum").textbox('getValue');
		var number = $("#tally_chargeProjectNumber").numberspinner('getValue');
		$("#tally_Detial_Table").datagrid('appendRow', {
			chargecodeid : row.CHARGECODE,
			chargename : row.CHAEGENAME,
			feetype : row.CHAEGETYPENAME,
			unit3 : row.UNIT3,
			price3 : row.PRICE3,
			number : number,
			sum : total,
			tallydetailsstatus : '未核算',
			admin4 : '李逸'
		});
		$("#chargeProject_Table").datagrid('unselectAll');
		$("#tally_chargeProjectName").textbox('clear');
		$("#tally_chargeProjectNumber").numberspinner('setValue', 1);
		$("#tally_chargeProjectPrice").textbox('clear');
		$("#tally_chargeProjectSum").textbox('clear');

		var tallyValue = $("#tallyTotal").val() * 1 + total * 1;
		$("#tallyTotal").val(tallyValue);
	}
}
/**
 * 删除账单详情
 */
function deleteTallyDetialFromTallyPage(){
	var row  = $("#tally_Detial_Table").datagrid('getSelected');
	if(row==null){
		$.messager.alert('提示', '请先选择要删除的收费项目!', 'info');
	}else{
		var index= $("#tally_Detial_Table").datagrid('getRowIndex',row);
		$("#tally_Detial_Table").datagrid('deleteRow',index);
		var tallyValue = $("#tallyTotal").val() * 1 - row.sum* 1;
		$("#tallyTotal").val(tallyValue);
	}
}
/**
 * 提交添加账单表单
 */
function addTallyFrom(){
	var rows = $("#tally_Detial_Table").datagrid("getRows");
	var info = $("#addTally_form").form("validate");
	if(rows==''||info==false){
		$.messager.alert('提示','请先完善账单和账单零细信息！','info');
	}else{
		$("#addTally_form").form({
			onSubmit: function(param){
				for(var i = 0; i < rows.length; i ++){
					param['tallydetailsTables[' + i +'].chargecodeid'] = rows[i].chargecodeid;
					param['tallydetailsTables[' + i +'].chargename'] = rows[i].chargename;
					param['tallydetailsTables[' + i +'].feetype'] = rows[i].feetype;
					param['tallydetailsTables[' + i +'].unit3'] = rows[i].unit3;
					param['tallydetailsTables[' + i +'].price3'] = rows[i].price3;
					param['tallydetailsTables[' + i +'].number'] = rows[i].number;
					param['tallydetailsTables[' + i +'].sum'] = rows[i].sum;
					param['tallydetailsTables[' + i +'].tallydetailsstatus'] = rows[i].tallydetailsstatus;
					param['tallydetailsTables[' + i +'].admin4'] = rows[i].admin4;
				}
				return $("#addTally_form").form("validate");
			},
			success: function(v){
				$("#addTally_form").form("reset");//重置表单
				
				var rows = $("#tally_Detial_Table").datagrid('getRows');//删除datagrid数据
		        for(var i=rows.length-1;i>=0;i--){
		        	$("#tally_Detial_Table").datagrid('deleteRow',i);
		        }
				createTallyID();//重新生成账单号
			}
		});
		$("#addTally_form").submit();
	}
}
/**
 * 重置tally表单
 */
function resetTallyPage(){
	$("#addTally_form").form("reset");//重置表单
	
	var rows = $("#tally_Detial_Table").datagrid('getRows');//删除datagrid数据
    for(var i=rows.length-1;i>=0;i--){
    	$("#tally_Detial_Table").datagrid('deleteRow',i);
    }
	createTallyID();//重新生成账单号、
	$("#tally_chargeProjectNumber").focus();
	
}
/**
 * 根据住院号初始化为核算账单
 */
function writeTallyNoAccountByLiveID(){
	var row = $("#NoAccountTally_Table").datagrid('getSelected');
	$("#NoAccountTally_LiveID").textbox('setValue',row.LIVEID8);
	$("#NoAccountTally_name").textbox('setValue',row.PATIENTNAME5);
	
	$("#NoAccountTallyByLiveID_Table").datagrid({
		url:'to_queryNoAcountTallyByLiveID',
		queryParams:{
			"liveID":row.LIVEID8
		}
	});
	}
/**
 * 创建一个窗口去核算账单
 */
var WindowToAccountTally = null;
function createWindowToAccountTally(index){
	
   $('#NoAccountTallyByLiveID_Table').datagrid('selectRow',index);
	var row = $('#NoAccountTallyByLiveID_Table').datagrid('getSelected');
	
	WindowToAccountTally =$("<div/>").dialog({
		title:'账单号:' + row.tallyid,
		href:'to_TallyDetialAccountW',
		width:850,
		height:480,
		modal:true,
		collapsible:false,
		minimizable:false,
		maximizable:false,
		resizable:false,
		iconCls:'icon-search',
		onClose:function(){
			$(this).dialog('destroy');
		},
		onLoad:function(){
			
			$("#tally_Detial_AccountTable").datagrid({
				url:'to_queryTallyDetialToAccount',
				queryParams:{
					"tallyID":row.tallyid
				}
			});
		}
	});
}
/**
 * 删除账单详细
 */

function deleteTallyDetial(index){
	
	$('#tally_Detial_AccountTable').datagrid('selectRow',index);
	var row = $('#tally_Detial_AccountTable').datagrid('getSelected');
	
	$.messager.confirm('提示','确认删除这条记录吗？',function(v){
		if(v){
			$.getJSON('to_deleteTallyDetialfromAccount',
					{"TallyDetailID":row.tallydetailsid,
					 "deleteSum":row.sum},
					function(v){
						$('#tally_Detial_AccountTable').datagrid('reload');//刷新字表记录
						$('#tally_Detial_AccountTable').datagrid({//刷新成功后
							onLoadSuccess:function(){
								var rowi = $('#tally_Detial_AccountTable').datagrid('getRows');
								if(rowi==''){
									WindowToAccountTally.dialog('destroy');
								}
							}
						});
						$('#NoAccountTallyByLiveID_Table').datagrid('reload');//刷新主表
						$('#NoAccountTallyByLiveID_Table').datagrid({//。成功后
							onLoadSuccess:function(){
								var row2 = $('#NoAccountTallyByLiveID_Table').datagrid('getRows');
								if(row2==''){
									$('#NoAccountTally_Table').datagrid('reload');
									$('#NoAccountTally_LiveID').textbox('reset');
									$('#NoAccountTally_name').textbox('reset');
								}
							}
						});
						if(v.success==true){
							$.messager.alert('提示','操作成功！','info');
						}else{
							$.messager.alert('提示','操作失败！','info');
						}
			});
		}
	});
}
/**
 * 点击核算
 */
function Startaccount(index){
	$('#tally_Detial_AccountTable').datagrid('selectRow',index);
	var row = $('#tally_Detial_AccountTable').datagrid('getSelected');
	
	var TallyDetailID = row.tallydetailsid;
		$.getJSON('to_StartAccountTallyDetialfromAccount',
				{"TallyDetailID":row.tallydetailsid},
			function(v){
				$('#tally_Detial_AccountTable').datagrid('reload');//刷新字表记录
				$('#tally_Detial_AccountTable').datagrid({//刷新成功后
					onLoadSuccess:function(){
						var rowi = $('#tally_Detial_AccountTable').datagrid('getRows');
						if(rowi==''){
							WindowToAccountTally.dialog('destroy');//销毁窗口
							//当全部核算完了发送请求修改账单状态
							$.getJSON('to_changeTallyStatus',{
								"TallyDetailID":TallyDetailID
							},function(v){
								//修改完后刷新
								$('#NoAccountTallyByLiveID_Table').datagrid('reload');//刷新NoAccountTallyByLiveID_Table
								$('#NoAccountTallyByLiveID_Table').datagrid({//刷新后
									onLoadSuccess:function(){
										var row2 = $('#NoAccountTallyByLiveID_Table').datagrid('getRows');
										if(row2==''){
											$('#NoAccountTally_Table').datagrid('reload');
											$('#NoAccountTally_LiveID').textbox('reset');
											$('#NoAccountTally_name').textbox('reset');
										}
									}
								});
							});
						}
					}
				});
		});
}
/**
 * 创建住院查询窗口for缴费
 */
var LiveInWindowForPay;
function createLiveInWindowForPay(){
	LiveInWindowForPay = $("<div/>").window({
		title:'住院查询',
		href:'to_queryPatientByLike_forTally',
		width:950,
		height:500,
		modal:true,
		collapsible:false,
		minimizable:false,
		maximizable:false,
		resizable:false,
		iconCls:'icon-search',
		onClose:function(){
			$(this).dialog('destroy');
		}
	});
	$("#LiveInSelect").window("options").SelectLiveIn = "toLiveInPay";
}
/**
 * 将信息回填到住院缴费界面
 */
function returnLiveInInfoToPayTalltPage(){
	var row = $("#livePatient_queryTable").datagrid("getSelected");
	
	$("#PayTallyPageLiveID").textbox('setValue',row.liveid8);
	$("#PayTallyPageName").textbox('setValue',row.patientname8);
	$("#PayTallyPageOffice").textbox('setValue',row.liveoffice4);
	
	$("#PayTallyPage_NopayTable").datagrid({
		url:'to_queryCanPayTally',
		queryParams:{
			"liveID":row.liveid8
		}
	});
	$.getJSON('to_queryBalance',{"liveID":row.liveid8},
			function(data){
		$("#PayTallyPageDeposit").textbox('setValue', data.deposit);
		$("#PayTallyPageAlltally").textbox('setValue', data.alltally);
		$("#PayTallyPageHasPay").textbox('setValue', data.haspay);
		$("#PayTallyPageNoPay").textbox('setValue', data.nopay);
	});
	LiveInWindowForPay.dialog('destroy');
}
/**
 * 创建新的缴费单号
 */
function createNewPayID(){
	$.getJSON('to_createNewPayId',{},function(v){
		$("#PayTallyPagePayID").textbox('setValue',v);
	});
}
/**
 * 根据账单刷新零细
 * @param tallyid
 */
function bookDetialInfo(tallyid){

	$("#PayTallyPage_NopayDetialTable").datagrid({
		url:'to_queryTallyDetialToPay',
		queryParams:{
			"tallyID":tallyid
		}
	});
}
/**
 * 计算支付总价
 */
function countPayTotalforAdd(){
		$("#PayTallyPageTotal").textbox('setValue',0);
		var initCount = $("#PayTallyPageTotal").textbox('getValue');
		var rows = $("#PayTallyPage_NopayTable").datagrid('getSelections');
		if(rows==''){
			$("#PayTallyPageTotal").textbox('setValue',0);
		}else{
			for(var i=0;i<rows.length;i++){
				initCount =rows[i].tallytotal*1 + initCount*1;
			}
			$("#PayTallyPageTotal").textbox('setValue',initCount);
		}
		
}
/**
 * 提交表单新增缴费
 */
function addnewPayTally() {
	var rows = $("#PayTallyPage_NopayTable").datagrid('getSelections');
	if(rows==''){
		$.messager.alert('提示','请先添加缴费账单！','info');
	}else{
		$("#to_addNewPayTally_form").form({
			onSubmit : function(param) {
					for (var i = 0; i < rows.length; i++) {
						param['tid['+ i+']'] = rows[i].tallyid;}
					return $("#to_addNewPayTally_form").form("validate");
					},
					success : function(v) {
						if(v=true){
							$.messager.alert('提示','缴费成功！','info');
							initPayPage();
						}else{
							$.messager.alert('提示','操作失败！','info');
						}
					}
			});
		$("#to_addNewPayTally_form").submit();
		
	}
}
/**
 * 提交后初始化界面
 */
function initPayPage(){
	createNewPayID();
	var rows1 = $("#PayTallyPage_NopayTable").datagrid('getRows');//删除datagrid数据
	if(rows1!=''){
		for(var i=rows1.length-1;i>=0;i--){
	    	$("#PayTallyPage_NopayTable").datagrid('deleteRow',i);
	    }
	}
    var rows2 = $("#PayTallyPage_NopayDetialTable").datagrid('getRows');//删除datagrid数据
    if(rows2!=''){
    	for(var i=rows2.length-1;i>=0;i--){
    	    	$("#PayTallyPage_NopayDetialTable").datagrid('deleteRow',i);
    	 }
    }
    $("#PayTallyPageLiveID").textbox('clear');
    $("#PayTallyPageName").textbox('clear');
    $("#PayTallyPageOffice").textbox('clear');
    $("#PayTallyPageDeposit").textbox('clear');
    $("#PayTallyPageAlltally").textbox('clear');
    $("#PayTallyPageHasPay").textbox('clear');
    $("#PayTallyPageNoPay").textbox('clear');
    $("#PayTallyPageTotal").textbox('clear');
}

var SelectLiveInToAllFees;
function createSelectLiveInWindowToAllFees(){
	SelectLiveInToAllFees = $("<div/>").window({
		title:'住院查询',
		href:'to_queryPatientByLike_forTally',
		width:850,
		height:500,
		modal:true,
		collapsible:false,
		minimizable:false,
		maximizable:false,
		resizable:false,
		iconCls:'icon-search',
		onClose:function(){
			$(this).dialog('destroy');
		}
	});
	$("#LiveInSelect").window("options").SelectLiveIn = "toFees";
}

function returnInfoToAllFeesPage(){
	var row = $("#livePatient_queryTable").datagrid("getSelected");
	$("#ttt_id").html(row.liveid8);
	$("#ttt_name").html(row.patientname8);
	$("#ttt_status").html(row.livestatus);
	$("#ttt_office").html(row.liveoffice4);
	$("#ttt_sex").html(row.patientsex);
	$("#ttt_age").html(row.patientage);
	$("#ttt_intime").html(row.livedate);
	$("#ttt_type").html(row.livetypename);
	
	$.getJSON('to_queryBalance',{"liveID":row.liveid8},
			function(data){
		$("#ttt_deposit").html(data.deposit);
		$("#ttt_allspent").html(data.alltally);
		$("#ttt_haspay").html(data.haspay);
		$("#ttt_nopay").html(data.nopay);
		$("#ttt_balance").html(data.balance);
	});
	
	SelectLiveInToAllFees.dialog('destroy');
}




